#include<bits/stdc++.h>
using namespace std;
int t,n,m,k=1,c[100001],a[100001],x[4];
map<int,int>d[100001];
int main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    scanf("%d%d",&n,&m);
    for (int i=1;i<=n;i++) scanf("%d",&a[i]);
    for (int i=1;i<=m;i++) scanf("%d",&c[i]);
    for (int i=1;i<=100;i++){
        if (k>=n) break;
        k*=2;
    }
    for (int i=1;i<=k;i++) for (int j=1;j<=pow(2,k-i);j++) scanf("%d",&d[i][j]);
    scanf("%d",&t);
    for (;t;t--){
        for (int i=0;i<4;i++) scanf("%d",&x[i]);
        printf("%d",x[0]^x[1]^x[2]^x[3]);
    }
    return 0;
}
